package uiLayer;
import modelLayer.*;
import controlLayer.*;
import java.util.Scanner;
import java.util.ArrayList;

/**
 * A class that handles the UI of the Loan menu and interacts with the LoanController.
 * 
 * @author (Gruppe E) 
 * @version (06-11-2014)
 */
public class LoanUI
{
    LoanController loanController;
    Scanner keyboard;

    /**
     * Constructor for LoanUI.
     */
    public LoanUI(LoanController controller)
    {
        loanController = controller;
        keyboard = new Scanner(System.in);
    }

    /**
     * starts the loan UI.
     */
    public void start()
    {
        loanMenu();
    }

    /**
     * Runs a while-loop, prompting the user for input and calls the sub-menus.
     */
    private void loanMenu()
    {
        boolean exit = false;

        while(!exit)
        {
            String choice = writeLoanMenu();
            if(choice.equals("1"))
            {
                makeLoan();
            }

            else if(choice.equals("2"))
            {
                findLoan();
            }

            else if(choice.equals("3"))
            {
                updateLoan();
            }

            else if(choice.equals("4"))
            {
                deleteLoan();
            }

            else if(choice.equals("5"))
            {
                showAllLoans();
            }
            else if(choice.equals("6"))
            {
                exit = true;
            }

            else
            {
                System.out.println("\nInvalid types. Please try again.");
            }
        }
    }

    /**
     * Writes Strings of choices and takes input from the user
     * @return  int
     */

    private String writeLoanMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        blankLines();
        System.out.println("*** LoanMenu ***");
        System.out.println("1. Make Loan");
        System.out.println("2. Find Loan");
        System.out.println("3. Update Loan"); 
        System.out.println("4. Delete Loan");
        System.out.println("5. Show Loans");
        System.out.println("6. Back");
        System.out.println("\nPlease choose a command");
        String choice = keyboard.nextLine();
        return choice;
    }

    /**
     * Requests the loanController to return a loan. If it exists prints it out, else
     * notifies the user that a loan with the ID does not exist.
     */
    private void findLoan()
    {
        blankLines();
        System.out.println("\nWhich loan do you wish to update?");
        String id = inputID();
        Loan loan = loanController.findLoan(id);
        if(loan != null)
        {
            String text = "";
            System.out.println("\nLoan found.\n");
            text += "Loaner Name: " + loan.getPerson().getName() + "\n";
            text += "Phone number: " + loan.getPerson().getPhone() + "\n";
            text += "DVD Title: " + loan.getCopy().getDvd().getTitle() + "\n";
            text += "Borrow Date: " + loan.getBorrowDate() + "\n";
            text += "Period: " + loan.getPeriod() + "\n";
            text += "Serial Number: " + loan.getCopy().getSerialNumber() + "\n";
            System.out.println(text);
        }

        else
        {
            System.out.println("\nLoan not found.\n");
        }
    }

    /**
     * Creates a loan with parameter-inputs from the user.
     * Person and copy matching the input values for the loan to be sucesfully updated. 
     */
    private void makeLoan()
    {
        blankLines();
        String id = inputID();
        String borrowDate = inputBorrowDate();
        String period = inputPeriod();
        String phone = inputPhone();
        String barcode = inputBarcode();
        String serial = inputSerial();
        String returnText = loanController.createLoan(id, borrowDate, period, phone, barcode, serial);

        if(returnText.equals("personNotFound"))
        {
            System.out.println("\nPerson with that phonenumber doesn't exist");
        }
        else if(returnText.equals("DVDNotFound"))
        {
            System.out.println("\nThat copy of the DVD doesn't exist");
        }
        else if(returnText.equals("DVDRentOut"))
        {
            System.out.println("\nThat DVD is rent out already");
        }
        else if(returnText.equals("loanExists"))
        {
            System.out.println("\nLoan already exists");
        }
        else if(returnText.equals("confirmed"))
        {
            System.out.println("\nLoan succesfully created!");
        }
    }

    /**
     * Finds a Loan with a ID-attribute matching the user input.
     * If it exists: Prints information about the Loan. 
     */
    private void updateLoan()
    {   
        blankLines();
        System.out.println("\nWhich loan do you wish to update?");
        String id = inputID();

        if(loanController.findLoan(id) != null)
        {
            System.out.println("\nLoan found. Enter the new informations.\n");
            String newId = inputID();
            String borrowDate = inputBorrowDate();
            String period = inputPeriod();
            loanController.updateLoan(id, borrowDate, period, newId);
        }
        else
        {      
            System.out.println("\nLoan not found");
        }

    }

    /**
     * Requests the LoanController to delete a loan if the loan exists.
     * Else prints a "failed" message.
     */
    private void deleteLoan()
    {
        blankLines();
        System.out.println("\nWhich loan do you wish to delete?");
        String id = inputID();

        if(loanController.deleteLoan(id))
        {
            System.out.println("\nLoan sucesfully deleted.\n");
        }
        else
        {
            System.out.println("\nLoan with that ID does not exist.");
        }
    }

    /**
     * Requests the arrayList of loans from the LoanController and prints a history of titles of loaned DVDs.
     */
    private void showAllLoans()
    {
        blankLines();
        String text = "List of loaned DVDs\n";
        ArrayList<Loan> loans = loanController.getLoans();
        for(int i=0; i < loans.size();i++)
        {
            text += loans.get(i).getCopy().getDvd().getTitle() + "\n";
        }
        System.out.println(text);
    }
    
    /**
     * Prompts the user to enter an ID string.
     * @return  String 
     */
    private String inputID()
    {
        System.out.println("\nEnter the loan ID");
        String id = keyboard.nextLine();
        return id;
    }

    /**
     * Prompts the user to enter a barode string.
     * @return  String 
     */
    private String inputBarcode()
    {
        System.out.println("\nEnter the barcode of the DVD");
        String id = keyboard.nextLine();
        return id;
    }

    /**
     * Prompts the user to enter an borrow-date string.
     * @return  String 
     */
    private String inputBorrowDate()
    {
        System.out.println("\nEnter the borrow-date of the loan");
        String id = keyboard.nextLine();
        return id;
    }

    /**
     * Prompts the user to enter an period string.
     * @return  String 
     */
    private String inputPeriod()
    {
        System.out.println("\nEnter the period of the loan");
        String id = keyboard.nextLine();
        return id;
    }

    /**
     * Prompts the user to enter an phonenumber string.
     * @return  String 
     */
    private String inputPhone()
    {
        System.out.println("\nEnter the phonenumber of the person lending the DVD");
        String id = keyboard.nextLine();
        return id;
    }

    /**
     * Prompts the user to enter a serial number string.
     * @return  String 
     */
    private String inputSerial()
    {
        System.out.println("\nEnter the serial number of the copy");
        String id = keyboard.nextLine();
        return id;
    }

    /**
     * This method adds 5 blank lines.
     */
    private void blankLines()
    {
        for(int i = 0; i < 5; i++)
        {
            System.out.println();
        }
    }
}
